﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class TheaterController : Controller
    {
        private TheaterBLO _theaterBLO = new TheaterBLO();

        /// <summary>
        /// Load list of Theater
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.Count = 0;
            List<Theater> listTheater = new List<Theater>();
            listTheater = _theaterBLO.GetAllTheaterInfo();
            return View(listTheater);
        }

        /// <summary>
        /// Go to Create page
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            ViewBag.TheaterName = null;
            IEnumerable<User> listUser = new List<User>();
            listUser = _theaterBLO.GetAllSupplier();
            ViewBag.ListSupplier = new SelectList(listUser.ToList(), "UserName", "FullName");
            return View();
        }

        /// <summary>
        /// Event Create new Theater
        /// </summary>
        /// <param name="theater"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Theater theater, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!_theaterBLO.IsExistTheater(theater))
                    {
                        if (file != null)
                        {
                            file.SaveAs(HttpContext.Server.MapPath("~/Image/Theater/") + file.FileName);
                            theater.ImageURL = file.FileName;
                        }

                        ViewBag.TheaterName = null;
                        _theaterBLO.CreateTheater(theater);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewBag.TheaterName = theater.TheaterName;
                        ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                        return View(theater);
                    }
                }
                catch (Exception)
                {
                    ViewBag.TheaterName = theater.TheaterName;
                    ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                    return View(theater);
                }
            }
            else
            {
                ViewBag.TheaterName = theater.TheaterName;
                ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                return View(theater);
            }
        }

        /// <summary>
        /// Go to Edit page
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            Theater theater = _theaterBLO.GetTheaterById(id);
            ViewBag.TheaterName = null;
            ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
            if (theater == null)
            {
                return HttpNotFound();
            }
            return View(theater);
        }

        /// <summary>
        /// Event Create new Theater
        /// </summary>
        /// <param name="theater"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Theater theater, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Theater theaterEntity = new Theater();
                    theaterEntity = _theaterBLO.GetTheaterById(theater.TheaterId);
                    if (!theater.TheaterName.Equals(theaterEntity.TheaterName))
                    {
                        if (!_theaterBLO.IsExistTheater(theater))
                        {
                            if (file != null)
                            {
                                if (file.FileName.Equals(theaterEntity.ImageURL))
                                {
                                    file.SaveAs(HttpContext.Server.MapPath("~/Image/Theater/") + file.FileName);
                                    theater.ImageURL = file.FileName;
                                }
                                else if (!file.FileName.Equals(theaterEntity.ImageURL))
                                {
                                    System.IO.File.Delete(HttpContext.Server.MapPath("~/Image/Theater/") + theaterEntity.ImageURL);
                                    file.SaveAs(HttpContext.Server.MapPath("~/Image/Theater/") + file.FileName);
                                    theater.ImageURL = file.FileName;
                                }
                            }

                            ViewBag.TheaterName = null;
                            _theaterBLO.EditTheater(theater);
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            ViewBag.TheaterName = theater.TheaterName;
                            ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                            return View(theater);
                        }
                    }
                    else
                    {
                        if (file != null)
                        {
                            if (file.FileName.Equals(theaterEntity.ImageURL) || string.Empty.Equals(theaterEntity.ImageURL))
                            {
                                file.SaveAs(HttpContext.Server.MapPath("~/Image/Theater/") + file.FileName);
                                theater.ImageURL = file.FileName;
                            }
                            else if (!file.FileName.Equals(theaterEntity.ImageURL))
                            {
                                System.IO.File.Delete(HttpContext.Server.MapPath("~/Image/Theater/") + theaterEntity.ImageURL);
                                file.SaveAs(HttpContext.Server.MapPath("~/Image/Theater/") + file.FileName);
                                theater.ImageURL = file.FileName;
                            }
                        }

                        ViewBag.TheaterName = null;
                        _theaterBLO.EditTheater(theater);
                        return RedirectToAction("Index");
                    }
                }
                catch (Exception)
                {

                    ViewBag.TheaterName = theater.TheaterName;
                    ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                    return View(theater);

                }

            }
            else
            {
                ViewBag.TheaterName = theater.TheaterName;
                ViewBag.ListSupplier = new SelectList(_theaterBLO.GetAllSupplier(), "UserName", "FullName", theater.UserName);
                return View(theater);
            }
        }

    }
}
